package com.pkg.login;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;



import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
    private String password;
    public static int countUser = 0;
    SessionMap<String,Object> sessionMap; 
    
    public  ArrayList<String> userlist2 = new ArrayList<String>();
    
	public String execute() {
    	String status = "error";
    	Map<String, Object> session = ActionContext.getContext().getSession();
    	if ((this.username.equals("admin") || this.username.equals("test1")|| this.username.equals("test2"))
                && (this.password.equals("admin") || this.password.equals("test1")|| this.password.equals("test2"))) {
    		if(Constants.getUserList()!= null){
    			int c = 0;
    			for(String s : Constants.getUserList()){
    				 if(this.username.equalsIgnoreCase(s)){
    					 c = 1;
    	                	break;
    				 }
    			}
    			
    			if(c != 1){
    				Constants.usercount =  Constants.getUsercount() + 1;
                	session.put("count",Constants.usercount);
                	Constants.getUserList().add(this.username);
                	session.put("curUser",this.username);
                	session.put("userlist",Constants.getUserList() );
    			}
    		}else{
    			
    			Constants.setUserList( new ArrayList<String>());
    			Constants.usercount =  Constants.getUsercount() + 1;
            	session.put("count",Constants.usercount);
            	session.put("curUser",this.username);
            	Constants.getUserList().add(this.username);
            }
    		userlist2 = (ArrayList<String>) Constants.getUserList();
    		Collections.reverse(userlist2);
    		 Constants.setLatestUserList(userlist2);
    		status = "success";
    	}else {
        	status = "error";
        }
    	return status;
    }
	
	
	public void setSession(Map map){
		sessionMap = (SessionMap)map;
		sessionMap.put("login", "true");
	}
	
	public String logout(){
		String usr = (String) sessionMap.get("curUser");
		int curCount = (int) sessionMap.get("count");
		for(String s : Constants.getUserList()){
			 if(usr.equalsIgnoreCase(s)){
				 Constants.getUserList().remove(s);
				 if(curCount > 0){
					 Constants.usercount = Constants.getUsercount() - 1;
				 }
               	break;
			 }
		}
		sessionMap.invalidate();
		
		return SUCCESS;
	}
	
	
	public ArrayList<String> getUserlist2() {
		return userlist2;
	}
	public void setUserlist2(ArrayList<String> userlist2) {
		this.userlist2 = userlist2;
	}
	
	
 public int getCountUser(){
	 return countUser;
 }
    
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
}
